package com.wonderful.eyepetizer.app.ktx

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.pager.PagerState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.util.lerp
import kotlin.math.absoluteValue

/**
 * Desc:
 * @Author: wonderful
 * @Time: 2024/1/24 16:32
 */

/**
 * 轮播视觉过渡效果
 */
@OptIn(ExperimentalFoundationApi::class)
fun Modifier.visualTransition(page: Int, pagerState: PagerState) = graphicsLayer {
    // 计算当前页面和目标页面之前的偏移量
    val pageOffset =
        ((pagerState.currentPage - page) + pagerState.currentPageOffsetFraction).absoluteValue
    // 使用偏移量在 0.7 和 1 之前插值出一个转换值
    // coerceIn 函数可以确保分数保持在0到1的范围内
    val transformation = lerp(start = 0.7f, stop = 1f, fraction = 1f - pageOffset.coerceIn(0f, 1f))
    alpha = transformation
    scaleY = transformation
}